home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11626 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  102 lines

  1. Path: acs5.bu.edu!lachesis
  2. From: lachesis@bu.edu (wai yip)
  3. Newsgroups: comp.lang.c
  4. Subject: pointers and functions
  5. Date: 25 Mar 1996 15:23:01 GMT
  6. Organization: Boston University
  7. Message-ID: <4j6dol$58g@news.bu.edu>
  8. NNTP-Posting-Host: acs5.bu.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. can someone help me find the bug in this program?
  12. this is the file the program reads from:
  13. 0 1
  14. 5 1
  15. 4 1
  16. * ***
  17. * ***
  18. * ***
  19. * ***
  20.  
  21. and here is the part of the program:
  22.  
  23. #include <stdio.h>
  24.  
  25. typedef struct{
  26.    int W;
  27.    int H;
  28.    int Sr;
  29.    int Sc;
  30.    int Er;
  31.    int Ec;
  32.    int Max;
  33.    char *mazePtr;
  34.    }mazeType;
  35.  
  36. mazeType *readMaze(char *fname);
  37. int printMaze(mazeType *maze);
  38.  
  39.  
  40. mazeType *readMaze(char *fname)
  41. {
  42. FILE *ifp;
  43. char c,*Array=NULL;
  44. int x=0;
  45. mazeType Maze;
  46. mazeType *StructPtr=NULL;
  47.  
  48. ifp=fopen(fname,"r");
  49. fscanf(ifp,"%d%d",&Maze.W,&Maze.H);
  50. fscanf(ifp,"%d%d",&Maze.Sr,&Maze.Sc);
  51. fscanf(ifp,"%d%d",&Maze.Er,&Maze.Ec);
  52.  
  53. if ((Array=(char *)malloc(sizeof(char)*Maze.W*Maze.H)) == NULL) 
  54.    exit(1);
  55. while ((c=getc(ifp)) != EOF)
  56.    {
  57.     Array[x]=c;
  58.     x++;
  59.    }
  60. printf("X=Max=%d\n",x);
  61. Maze.Max=x;
  62. Maze.mazePtr=Array;
  63. StructPtr=&Maze;
  64. return (StructPtr);
  65. }
  66.  
  67. int printMaze(mazeType *maze)
  68. {
  69. int x;
  70.  
  71. if (maze == NULL) return 0;
  72. printf("mazeWidth= %d   mazeHeight=  %d\n",maze->W,maze->H);
  73. printf("startRow=  %d   startColumn= %d\n",maze->Sr,maze->Sc);
  74. printf("endRow=    %d   endColumn=   %d\n",maze->Er,maze->Ec);
  75. printf("maze size is: %d",maze->Max);
  76.  
  77. for (x=0;x<maze->Max;x++)
  78.    printf("%c",maze->mazePtr[x]);
  79. printf("\n");
  80.  
  81. return 1;
  82. }
  83.  
  84.  
  85. void main(int argc,char *argv[])
  86. {
  87. mazeType *StructPtr=NULL;
  88.  
  89. StructPtr=readMaze(argv[1]);
  90.  
  91. if (!printMaze(StructPtr))
  92.    {
  93.     printf("Cannot print maze because it is NULL\n");
  94.     exit (1);
  95.    }
  96. }
  97.  
  98. the problem with this program is that it doesn't print some of the data out
  99. correctly and doesn't print out the maze at all when passed to the function.
  100. It printed everything out correctly in main when i tried it, but not in the
  101. function... which leads me to believe i'm calling it incorrectly.
  102.